package com.corejsf;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named("service")
@ApplicationScoped
public class Service implements Serializable {
	
	@Inject
	private User currentUser;
	@Inject
	private Dao d;
	
	public int postCount(User u) {
		return d.gPostCount(u);
	}
	
	public List<User> users() {
		return d.getUsers();
	}
	
	public List<Category> getCategories() {
		return d.getCategories();
	}
	
	public List<Thread> threads(Category cat) {
		return d.getThreads(cat);
	}
	
	public List<Post> posts(Thread thr) {
		return d.getPosts(thr);
	}

	public String showCategory(Category cat) {
		currentUser.setOpenCategory(cat);
		return "categori";
	}
	
	public String showThread(Thread thr) {
		currentUser.setOpenThread(thr);
		return "thread";
	}
	
	public String verify() {
		
		for(User u : d.getUsers()) {
			if(currentUser.getNick().equals(u.getNick()) && currentUser.getPass().equals(u.getPass())) {
				currentUser.setLevel(u.getLevel());
				currentUser.setId(u.getId());
			} else {
				
			}
		}
		
		return "index";
	}
	
	public void createUser() {
		d.createUser(currentUser);
	}
	
	public void deleteCategory(Category cat) {
		d.deleteCategory(cat);
	}
	
	public void deletePost(Post p) {
		d.deletePost(p);
	}
	
	public void deleteThread(Thread t) {
		d.deleteThread(t);
	}
}
